let _Template;
let _View;
let _Model;
let _ButtonList = new Array();
let _IntervalList = new Array();

$(function () {

    bind();
    search();

    _Model = new loadModel();

    loadMethod();
});

//绑定控件
var bind = function () {

    try {
    }
    catch (e) {
        alert(e);
    }
}

//查询模板
var search = function () {
    try {
        //获取页面参数
        var vPublishID = HttpUtil.getQueryString("target")

        if (vPublishID != undefined && vPublishID != null) {

            let xheList = new XheList();

            xheList.model.Table = "t_data_publish";
            xheList.model.SortName = "publishid";
            xheList.model.Columns = [
                ["publishcode", "模板代码", "15", "", "C", "", true],
                ["publishbackground", "背景图", "15", "", "C", "", true],
            ];
            xheList.model.Filter = [
                ["publishid", "A", "EQ", "I", vPublishID],
                ["publishstatus", "A", "EQ", "I", 1]
            ];

            let vResult = xheList.query();

            if (vResult != null && vResult.rows != null && vResult.rows.length > 0) {
                let vTemplateCode = GZip.decompress(decodeURIComponent(String(vResult.rows[0]["publishcode"]).replace(/&apos/g, "'")));

                _Template = JSON.parse(vTemplateCode);

                if (vResult.rows[0]["publishbackground"] != "") {

                    document.getElementById("div_background").style.backgroundImage = "url(../../common/imgs/" + vResult.rows[0]["publishbackground"] + ")";
                }
            }
        }
    }
    catch (e) {
        alert(e);
    }
}

//加载模块
let loadModel = function () {

    try {
        this.tempIndex = 0;
        this.tempData = _Template;

        let options = {};
        $('#grid-stack').gridstack(options);

        this.grid = $('#grid-stack').data('gridstack');

        this.load_grid = function () {
            try {

                this.grid.remove_all();

                _.each(this.tempData, function (node) {

                    var _index = node.index;

                    let _model = new xheModel();
                    let _echarts = new xheCharts();

                    _model.options = node.model.options;
                    _echarts.options = node.echart.options;

                    let vTemplate = `
                    <div>
                        <div id="div_content_${_index}" class="grid-stack-item-content boxall" style="overflow-y: hidden;">                          
                            <h1 id="h_head_${_index}" class="grid-stack-item-tl">
                                <div id="div_head_${_index}" class="grid-stack-item-dv">                                     
                                    <span>&nbsp;&nbsp;</span>
                                    <span id="spn_title_${_index}" class="grid-stack-item-sp"></span>                                
                                </div>        
                            </h1>
                            <div id="div_model_${_index}" class="grid-stack-item-nr"></div>
                            <div id="div_foot_${_index}" class="boxfoot"></div>
                        </div>
                    </div>`;

                    this.grid.add_widget($(vTemplate), node.x, node.y, node.width, node.height, undefined, node.index, false, false);

                    $('#remove-grid-' + _index).click(this.remove_grid);
                    $('#config-grid-' + _index).click(this.config_grid);


                    //_model.options.index = _index;

                    _echarts.options.control = "div_model_" + _index;

                    _model.load();

                    if (_echarts.options.type == eCharts.type.Button
                        || _echarts.options.type == eCharts.type.Text
                        || _echarts.options.type == eCharts.type.Select
                        || _echarts.options.type == eCharts.type.Date) {

                        _echarts.load();
                    }
                }, this);
            }
            catch (e) {
                alert(e)
            }
        }.bind(this);

        this.load_grid();
        this.grid.disable();
    }
    catch {
        alert(e);
    }
};

//单个按钮加载事件
function searchMethod(params) {

    try {

        if (String(params.event).indexOf("url:") > -1) {

            let url = "";
            let permission = "";

            if (String(params.event).indexOf("|") > -1) {

                url = String(params.event).split('|')[0].replace("url:", "").replaceAll("&amp;", "&").replace("{qxcode}", USER_INFO.OrganizeCode);
                permission = String(params.event).split('|')[1];
            }
            else {
                url = params.event.replace("url:", "").replaceAll("&amp;", "&").replace("{qxcode}", USER_INFO.OrganizeCode);
            }

            let aryUrlItem = url.split("{");

            url = "";

            for (let i = 0; i < aryUrlItem.length; i++) {

                if (aryUrlItem[i].indexOf("}") > -1) {
                    let controlID = "";
                    let format = "";

                    let params = aryUrlItem[i].split("}")[0]

                    if (params.indexOf(",") > -1) {
                        controlID = params.split(",")[0];
                        format = params.split(",")[1];
                    }
                    else {
                        controlID = params;
                    }

                    if (document.getElementById(controlID) != undefined && document.getElementById(controlID) != null) {
                        if (format != "") {
                            url += document.getElementById(controlID).value.replace(format, "") + aryUrlItem[i].split("}")[1];
                        }
                        else {
                            url += document.getElementById(controlID).value + aryUrlItem[i].split("}")[1];
                        }
                    }
                    else {
                        url += "{" + aryUrlItem[i];
                    }
                }
                else {
                    url += aryUrlItem[i];
                }
            }

            if (permission == "") {
                window.open(url);
            }
            else {

                if (judgeMatch(permission)) {

                    window.open(url);
                }
                else {

                    alert("没有权限!");
                }
            }
        }
        else {
            for (let i = 0; i < _Model.tempData.length; i++) {


                if (_Model.tempData[i].echart.options.searchControl != undefined) {

                    if (String(params.event + "|").indexOf(String(_Model.tempData[i].echart.options.searchControl.ID) + "|") > -1) {
                        var echart = new xheCharts();
                        echart.options = _Model.tempData[i].echart.options;
                        echart.load();
                    }
                }
            }
        }



    }
    catch (e) {
        alert(e);
    }
}

//所有按钮加载事件
function loadMethod() {
    try {

        for (let i = 0; i < _Model.tempData.length; i++) {

            if (_Model.tempData[i].echart.options.type != eCharts.type.Button
                && _Model.tempData[i].echart.options.type != eCharts.type.Text
                && _Model.tempData[i].echart.options.type != eCharts.type.Select
                && _Model.tempData[i].echart.options.type != eCharts.type.Date) {

                var echart = new xheCharts();
                echart.options = _Model.tempData[i].echart.options;
                echart.load();
            }

            if (_Model.tempData[i].echart.options.searchModel != undefined
                && _Model.tempData[i].echart.options.searchModel.period != undefined
                && _Model.tempData[i].echart.options.searchModel.period != 0) {

                let vTimer = setInterval("intervalMethod('" + JSON.stringify(_Model.tempData[i].echart.options) + "')", Number(_Model.tempData[i].echart.options.searchModel.period) * 1000);

                _IntervalList.push({ "key": _Model.tempData[i].echart.options.control, "event": vTimer });
            }
        }


    }
    catch (e) {

    }
}

//自动刷新的方法
function intervalMethod(options) {
    try {

        var echart = new xheCharts();
        echart.options = JSON.parse(options);
        echart.load();
    }
    catch (e) {

    }
}
